<?php

App::import('Model', 'Blockarea');


class BlockHelper extends AppHelper {
    
     var $static_blocks;
    
     function get_blocks(){
           
         
         if(($static_blocks = Cache::read('blocks', 'default')) === false) :
         
             $Blocks = new Blockarea();
             $tmp=$Blocks->find('all');
             $this->static_blocks=array();
             foreach($tmp as $item){
                 $this->static_blocks[$item['Blockarea']['name']]=$item;
             }
             Cache::write('blocks', $static_blocks, 'default');
         endif;
         
         
         
     }
     
     
     function display($area,$echo=true){
         
         
         if(!$this->static_blocks)
            $this->get_blocks();
         
            
         if(!isset($this->static_blocks[$area])) {
             die('Invalid Block Area:'.$area.'!');
         }
         else {
            $ret='';
            $pages=array();
            $allowed=false;
            if($tmp=$this->static_blocks[$area]['Blockarea']['pages']){
                $tmp=explode(',',trim(strtolower($tmp),','));
                
                foreach($tmp as $i){
                    if($i)
                        $pages[]=trim(Router::url().ltrim($i,'/'));
                }
                
                if(!count($pages)){
                    $allowed=true;
                }
            }
            else $allowed=true;
            
            if($allowed){
                if(count($this->static_blocks[$area]['Block'])){
                    foreach($this->static_blocks[$area]['Block'] as $item){
                        $ret.=$item['content'];
                    }
                }
            }
            elseif(in_array(Router::url(),$pages)){
                if(count($this->static_blocks[$area]['Block'])){
                    foreach($this->static_blocks[$area]['Block'] as $item){
                        $ret.=$item['content'];
                    }
                }
            }
         }
         
         if($echo)
            echo $ret;
         else return $ret;
     }
    
    
    
    
}
?>